<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * UserController
 * 
 * Userlist
 * 
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class UserController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	
	public function init()
	{		
		// registry
		$registry = Zend_Registry::getInstance();
		$this->registry = $registry;
		
		// auth
		$auth = Zend_Auth::getInstance();
    	$this->view->auth = $auth->hasIdentity();
    	
    	if($auth->hasIdentity())
    	{
    		$this->view->auth_role 			= $auth->getIdentity()->role;
    		$this->view->username 			= $auth->getIdentity()->username;
    		$this->view->auth_uid			= $auth->getIdentity()->uid;
    		$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
    	}
    	else
    	{
    		$this->view->username 	= $this->registry->get('lang')->_('guest');
    	}

    	$this->auth = $auth;
    	
		// lang
		$this->view->translate = Zend_Registry::get('lang');
		
		// flash messenger
		$this->view->flashMessenger	= $this->_helper->FlashMessenger;
	}
	
	
	
	/*
	 * Lists user
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// param
		$sort_by 	= $this->_request->getParam('sort_by', 'username');
		$sort_order	= $this->_request->getParam('sort_order', 'desc');
		$page 		= intval($this->getRequest()->getParam('page', 0));
		$count		= 25;
		
		// db
		$user		= new User();
		
		$valid_sort_by		= array('create_date', 'count_posts', 'username', 'forename', 'lastname', 'activity_points');
		$valid_sort_order	= array('asc', 'desc');
		
		if($sort_order && in_array($sort_by, $valid_sort_by))
		{
			$order = $sort_by;
		}
		
		if($valid_sort_order && in_array($sort_order, $valid_sort_by))
		{
			if(isset($order)) { $order .= ' ' . $sort_order; }
		}
		
		// query
		$query 		= $user->fetchAll('uid != 0 AND is_active=1', $order);
			
		// paginator
		$paginator	= Zend_Paginator::factory($query->toArray());
        $paginator->setItemCountPerPage($count);
        $paginator->setCurrentPageNumber($page);
        
		$this->view->count 		= $query->count();
		$this->view->results	= $paginator;
	}
	
	
	
	/*
	 * List of online users
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function onlineAction()
	{
		// param
		$page 		= intval($this->getRequest()->getParam('page', 0));
		$count		= 25;
		
		// db
		$userOnline	= new UserOnline();
		
		// fetch
		$select	= $userOnline->select()->setIntegrityCheck(false);
		$select->from(
			array('uo' => 'user_online')
		)
		->join(
			array('u' => 'user'), 'u.uid = uo.user_uid', array('username', 'role', 'group_uid')
		)
		->joinLeft(
			array('t' => 'threads'), 't.uid = uo.thread_uid', array('title AS thread_title')
		)
		->joinLeft(
			array('b' => 'boards'), 'b.uid = uo.board_uid', array('title AS board_title')
		)
		->order('uo.date DESC')
		->limit($count);
			
		$fetch	= $userOnline->fetchAll($select);
		
		// paginator
		$paginator	= Zend_Paginator::factory($fetch->toArray());
        $paginator->setItemCountPerPage($count);
        $paginator->setCurrentPageNumber($page);
        
        // view
		$this->view->count 		= $fetch->count();
		$this->view->results	= $paginator;
		
	}
}
?>